package com.yanshaohui.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.yanshaohui.springboot.entity.AddressEntity;
import com.yanshaohui.springboot.mapper.AddressMapper;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

@RestController
public class AddressController {

	@Autowired
	private AddressMapper addressMapper;
    
	@ApiOperation(value="创建地址")
    @ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PostMapping("/address")
	public int add(@RequestBody AddressEntity address){
		return addressMapper.insert(address);
	}

	@ApiOperation(value="更新地址")
    @ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PutMapping("/address")
	public String update(@RequestBody AddressEntity address){
		addressMapper.update(address);
		return "success";
	}
	
    @ApiOperation(value="获取地址详细信息", notes="根据地址名来获取地址详细信息")
    @ApiImplicitParam(name = "name", value = "地址", paramType="path",required = true, dataType = "String")
	@GetMapping("/address/{name}")
	public AddressEntity findAddress(@PathVariable("name") String name){
		return addressMapper.findByName(name);
	}

    @ApiOperation(value="删除地址")
    @ApiImplicitParam(name = "id", value = "Id", paramType="path",required = true, dataType = "Long")
	@DeleteMapping("/address/{id}")
	public String deleteAddress(@PathVariable("id") Long id){
		addressMapper.delete(id);
		return "success";
	}
    
    @ApiOperation(value="获取地址详细信息(属性可为空)", notes="根据地址名来获取地址详细信息")
	@GetMapping("/dyna/address")
	public List<AddressEntity> findAddress(AddressEntity entity){
		return addressMapper.dynaFindByName(entity);
	}
    
	@ApiOperation(value="创建地址(属性可为空)")
    @ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PostMapping("/dyna/address")
	public int dynaAdd(@RequestBody AddressEntity address){
		return addressMapper.dynaInsert(address);
	}
	
	@ApiOperation(value="更新地址(属性可为空)")
    @ApiImplicitParam(name = "address", value = "地址实体", required = true, dataType = "AddressEntity")
	@PutMapping("/dyna/address")
	public String dynaUpdate(@RequestBody AddressEntity address){
		addressMapper.dynaUpdate(address);
		return "success";
	}
}
